package com.uphone.kingmall.base;

import android.app.Activity;
import android.text.TextUtils;

import com.blankj.utilcode.util.ToastUtils;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.tencent.connect.UserInfo;
import com.tencent.connect.common.Constants;
import com.uphone.kingmall.activity.BindPhoneActivity;
import com.uphone.kingmall.activity.personal.set.BindAccountActivity;
import com.uphone.kingmall.app.MyApplication;
import com.uphone.kingmall.bean.LoginBean;
import com.uphone.kingmall.listener.onNormalRequestListener;
import com.uphone.kingmall.main.MainActivity;
import com.uphone.kingmall.utils.ActivityCollector;
import com.uphone.kingmall.utils.ConstansUtils;
import com.uphone.kingmall.utils.GsonUtils;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.OkGoUtils;
import com.uphone.kingmall.utils.SharedPreferencesHelper;
import com.uphone.kingmall.utils.intent.IntentUtils;

import org.json.JSONObject;

/**
 * <pre>
 *     author : radish
 *     e-mail : 15703379121@163.com
 *     time   : 2019/3/30
 *     desc   :
 * </pre>
 */
public class BaseUiListener extends BaseUiListener0 {
    public static final int TYPE_LOGIN = 0X0011;
    public static final int TYPE_BIND = 0X0022;
    private final Activity activity;
    private final int type;

    public BaseUiListener(Activity activity, int type) {
        this.activity = activity;
        this.type = type;
    }

    @Override
    protected void doComplete(JSONObject values) {
        initOpenidAndToken(values);
    }


    private void initOpenidAndToken(JSONObject jsonObject) {
        try {
            String token = jsonObject.getString(Constants.PARAM_ACCESS_TOKEN);
            String expires = jsonObject.getString(Constants.PARAM_EXPIRES_IN);
            final String openId = jsonObject.getString(Constants.PARAM_OPEN_ID);
            if (!TextUtils.isEmpty(token) && !TextUtils.isEmpty(expires)
                    && !TextUtils.isEmpty(openId)) {
                MyApplication.qqTencent.setAccessToken(token, expires);
                MyApplication.qqTencent.setOpenId(openId);
            }

            UserInfo mInfo = new UserInfo(activity, MyApplication.qqTencent.getQQToken());
            mInfo.getUserInfo(new BaseUiListener0() {
                @Override
                protected void doComplete(JSONObject values) {
                    try {
                        String nickname = "";
                        String qqImage = "";
                        if (values.has("nickname")) {
                            nickname = values.getString("nickname");
                        }
                        if (values.has("figureurl")) {
                            qqImage = values.getString("figureurl_qq_2");
                        }
                        if (type == TYPE_LOGIN) {
                            loginQQ(openId, nickname, qqImage);
                        } else if (type == TYPE_BIND) {
                            bindQQ(openId, nickname);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        } catch (Exception e) {
        }
    }

    private void bindQQ(String openId, String nickname) {
        HttpParams params = new HttpParams();
        params.put("qqid", openId);
        params.put("qqNo", nickname);
        OkGoUtils.normalRequest(MyUrl.bindQQ, params, new onNormalRequestListener() {
            @Override
            public void onSuccess(String body, int code) {
                if (code == 0) {
                    ToastUtils.showShort("绑定成功！");
                    BaseActivity activity = ActivityCollector.getActivity(BindAccountActivity.class);
                    if (activity != null && activity instanceof BindAccountActivity) {
                        ((BindAccountActivity) activity).setQQBind();
                    }
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }

    private void loginQQ(final String openId, final String nickname, final String qqImage) {
        HttpParams params = new HttpParams();
        params.put("qqId", openId);
        OkGoUtils.normalRequest(MyUrl.loginByQQ, params, new onNormalRequestListener() {
            @Override
            public void onSuccess(String body, int code) {
                if (code == 1) {
                    ActivityCollector.removeAll(MainActivity.class);
                    IntentUtils.getInstance()
                            .with(activity, BindPhoneActivity.class)
                            .putInt(ConstansUtils.TYPE, BindPhoneActivity.TYPE_QQ)
                            .putString("openid", openId)
                            .putString("nickname", nickname)
                            .putString("headimgurl", qqImage).start();
                    activity.finish();
                } else if (code == 0) {
                    ToastUtils.showShort("登录成功！");
                    LoginBean bean = GsonUtils.getGson().fromJson(body, LoginBean.class);
                    LoginBean.UserBean user = bean.getUser();
                    if (user == null) {
                        return;
                    }
//                    JpushUtil.setTagAndAlias(activity, user.getUserId());
                    SharedPreferencesHelper.saveToken(user.getToken());
                    SharedPreferencesHelper.saveUserName(user.getUserName());
                    SharedPreferencesHelper.saveUserPhone(user.getUserMobile());
                    SharedPreferencesHelper.saveUserId(user.getUserId());
                    SharedPreferencesHelper.saveUserImg(user.getUserPhoto());
//                    JpushUtil.setTagAndAlias(activity, user.getUserId());
//                    CommonUtil.loginRY();
                    ActivityCollector.removeAll(MainActivity.class);
                    activity.finish();
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }

}

